package com.zp.servlet;

import com.google.gson.Gson;
import com.zp.dao.UserDao;
import com.zp.dao.impl.UserDaoImpl;
import com.zp.utils.CommonUtil;
import com.zp.utils.ResultUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/user/delete")
public class DeleteUserServlet extends HttpServlet {

    private UserDao userDao;
    private PrintWriter out;

    @Override
    public void init() throws ServletException {
        userDao = new UserDaoImpl();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");

        out = response.getWriter();
        Gson gson = new Gson();

        String[] snoList = request.getParameterValues("snoList");
        Integer[] snos =  CommonUtil.stringToInteger(snoList);
        int row = userDao.deleteUsersBySnos(snos);
        if (row == snoList.length) {
            out.print(gson.toJson(ResultUtil.success()));
        }
    }

    @Override
    public void destroy() {
        super.destroy();
    }

}
